using System.Collections.Generic;

namespace Algorithm.LeetCode
{
    /// <summary>
    /// 第二十题：有效的括号
    /// </summary>
    public class No20
    {
        public static bool IsValid(string s)
        {
            var stack = new Stack<char>();
            foreach (var item in s)
            {
                switch (item)
                {
                    case '(' or '[' or '{':
                        stack.Push(item);
                        continue;
                    case ')' when !(stack.TryPop(out var c) && c == '('):
                    case ']' when !(stack.TryPop(out var c2) && c2 == '['):
                    case '}' when !(stack.TryPop(out var c3) && c3 == '{'):
                        return false;
                }
            }

            return stack.Count == 0;
        }
    }
}